package com.shrct.businessprocess.customerservice.impl;

import com.google.common.collect.Iterables;
import com.shrct.businessprocess.customerservice.RCProjectReference;
import com.shrct.businessprocess.customerservice.impl.RCServiceContractDelegateImpl;
import com.shrct.businessprocess.customerservice.proxy.RCProjectReferenceProxy;
import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.product.RCIdentifiablePhysicalProductEntry;
import com.shrct.businessprocess.product.RCLogicProduct;
import com.shrct.businessprocess.product.RCProductEntry;
import com.shrct.businessprocess.project.RCCustomerContract;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.impl.RCCustomerContractImpl;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;

@Entity
@DiscriminatorValue("RCPROJECTREFERENCE_TYPE")
public class RCProjectReferenceImpl extends RCServiceContractDelegateImpl implements RCProjectReference {
  public RCProjectReferenceImpl() {
    super();
  }
  
  @JoinColumn(name = "customerContract_id")
  @ManyToOne(targetEntity = RCCustomerContractImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCCustomerContract customerContract;
  
  public RCCustomerContract getCustomerContract() {
    return this.customerContract;
  }
  
  public RCProjectReference setCustomerContract(final RCCustomerContract customerContract) {
    this.customerContract = customerContract;
    return this;			
    
  }
  
  public RCEmployee updateBusinessMan() {
    RCEmployee _xblockexpression = null;
    {
      RCProject _project = this.customerContract.getProject();
      RCEmployee _businessMan = _project.getBusinessMan();
      this.setBusinessMan(_businessMan);
      RCEmployee _businessMan_1 = this.getBusinessMan();
      _xblockexpression = (_businessMan_1);
    }
    return _xblockexpression;
  }
  
  public RCEmployee updateMajorSales() {
    RCEmployee _xblockexpression = null;
    {
      RCProject _project = this.customerContract.getProject();
      RCEmployee _majorSales = _project.getMajorSales();
      this.setMajorSales(_majorSales);
      RCEmployee _majorSales_1 = this.getMajorSales();
      _xblockexpression = (_majorSales_1);
    }
    return _xblockexpression;
  }
  
  public Collection<RCLogicProduct> getShippingList() {
    Iterable<RCProductEntry> _allAcceptedProducts = this.customerContract.getAllAcceptedProducts();
    Iterable<RCIdentifiablePhysicalProductEntry> _filter = Iterables.<RCIdentifiablePhysicalProductEntry>filter(_allAcceptedProducts, RCIdentifiablePhysicalProductEntry.class);
    final Function1<RCIdentifiablePhysicalProductEntry,RCLogicProduct> _function = new Function1<RCIdentifiablePhysicalProductEntry,RCLogicProduct>() {
        public RCLogicProduct apply(final RCIdentifiablePhysicalProductEntry it) {
          RCLogicProduct _logicProduct = it.getLogicProduct();
          return _logicProduct;
        }
      };
    Iterable<RCLogicProduct> _map = IterableExtensions.<RCIdentifiablePhysicalProductEntry, RCLogicProduct>map(_filter, _function);
    List<RCLogicProduct> _list = IterableExtensions.<RCLogicProduct>toList(_map);
    return _list;
  }
  
  public static RCProjectReference create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.customerservice.RCProjectReference rCProjectReference = new com.shrct.businessprocess.customerservice.impl.RCProjectReferenceImpl(
    );
    objectFactory.create(rCProjectReference);
    return rCProjectReference;			
    
  }
  
  public static RCProjectReference createRCProjectReferenceImpl(final IObjectFactory objectFactory, final Procedure1<RCProjectReference> init) {
    com.shrct.businessprocess.customerservice.RCProjectReference rCProjectReference = new com.shrct.businessprocess.customerservice.impl.RCProjectReferenceImpl(
    );
    objectFactory.create(rCProjectReference);
    init.apply(rCProjectReference);
    
    return rCProjectReference;			
    
  }
  
  public RCProjectReferenceProxy toProxy() {
    com.shrct.businessprocess.customerservice.proxy.RCProjectReferenceProxy proxy = new com.shrct.businessprocess.customerservice.proxy.RCProjectReferenceProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
